﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLTV_BLL;
using QLTV_Public;

namespace QLTV_GiaoDien
{
    public partial class frm_ViPham : DevExpress.XtraEditors.XtraForm
    {
        public frm_ViPham()
        {
            InitializeComponent();
        }
        private int _MaViPham;

        public int MaViPham
        {
            get { return _MaViPham; }
            set { _MaViPham = value; }
        }
        private int _MaDocGia;

        public int MaDocGia
        {
            get { return _MaDocGia; }
            set { _MaDocGia = value; }
        }
        private string _LyDoViPham;

        public string LyDoViPham
        {
            get { return _LyDoViPham; }
            set { _LyDoViPham = value; }
        }
        private string _HinhThuc;

        public string HinhThuc
        {
            get { return _HinhThuc; }
            set { _HinhThuc = value; }
        }
        private string _NgayXuLy;

        public string NgayXuLy
        {
            get { return _NgayXuLy; }
            set { _NgayXuLy = value; }
        }
        private string _NguoiXuLy;

        public string NguoiXuLy
        {
            get { return _NguoiXuLy; }
            set { _NguoiXuLy = value; }
        }
        private int _Flag;

        public int Flag
        {
            get { return _Flag; }
            set { _Flag = value; }
        }
        public void Huy_Text(Control ctr)
        {
            if (ctr is TextBox || ctr is TextEdit)
                ctr.Text = string.Empty;
            foreach (Control i in ctr.Controls)
                Huy_Text(i);
        }
        public void EnabledText_True(Control ctr)
        {
            if (ctr is TextBox || ctr is TextEdit)
                ctr.Enabled =true;
            foreach (Control i in ctr.Controls)
                EnabledText_True(i);
        }
        public void EnabledText_False(Control ctr)
        {
            if (ctr is TextBox || ctr is TextEdit)
                ctr.Enabled = false;
            foreach (Control i in ctr.Controls)
                EnabledText_False(i);
        }
        public void GetData()
        {
            txt_MaVP.Text = MaViPham.ToString();
            txt_MaDocGia.Text = MaDocGia.ToString();
            txt_LyDo.Text = LyDoViPham;
            txt_HinhThuc.Text = HinhThuc;
            dateTime_NgayXuLy.Text = NgayXuLy;
            txt_NguoiXuLy.Text = NguoiXuLy;
        }
        public void LoadForm()
        {
            if (Flag == 0)
            {
                this.Text = "Thêm Mới Vi Phạm";
                bbi_Them.Enabled = false;
                bbi_Sua.Enabled = false;
                txt_MaVP.Enabled = false;
            }
            if (Flag == 1)
            {
                this.Text = "Sửa Vi Phạm";
                bbi_Them.Enabled = false;
                bbi_Sua.Enabled = false;
                GetData();
            }
            if (Flag == 2)
            {
                this.Text = "Xem Vi Phạm";
                bbi_Them.Enabled = false;
                bbi_Sua.Enabled = false;
                bbi_Luu.Enabled = false;
                EnabledText_False(this);
                GetData();
            }
        }
        public delegate void GetInt(int kt);
        public GetInt MyGetData;
        private void frm_ViPham_Load(object sender, EventArgs e)
        {
            LoadForm();
        }
        ViPham_BLL vipham_bll = new ViPham_BLL();
        private void bbi_Luu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ViPham_Public vp = new ViPham_Public();
            if (Flag == 0)
            {
                //them
                vp.MaDocGia=int.Parse(txt_MaDocGia.Text);
                vp.LyDoViPham = txt_LyDo.Text;
                vp.HinhThuc = txt_HinhThuc.Text;
                vp.NgayXuLy = Convert.ToDateTime(dateTime_NgayXuLy.Text);
                vp.NguoiXuLy = txt_NguoiXuLy.Text;
                vipham_bll.insert_ViPham(vp,vp,vp,vp,vp);
                XtraMessageBox.Show("Thêm mới thành công!","Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
            if (Flag == 1)
            {
                //Sửa
                vp.MaViPham = int.Parse(txt_MaVP.Text);
                vp.MaDocGia = int.Parse(txt_MaDocGia.Text);
                vp.LyDoViPham = txt_LyDo.Text;
                vp.HinhThuc = txt_HinhThuc.Text;
                vp.NgayXuLy = Convert.ToDateTime(dateTime_NgayXuLy.Text);
                vp.NguoiXuLy = txt_NguoiXuLy.Text;
                vipham_bll.Update_ViPham(vp,vp, vp, vp, vp, vp);
                XtraMessageBox.Show("Sửa thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            if (Flag == 2)
            {
                //xem
                vp.MaViPham = int.Parse(txt_MaVP.Text);
                vp.MaDocGia = int.Parse(txt_MaDocGia.Text);
                vp.LyDoViPham = txt_LyDo.Text;
                vp.HinhThuc = txt_HinhThuc.Text;
                vp.NgayXuLy = Convert.ToDateTime(dateTime_NgayXuLy.Text);
                vp.NguoiXuLy = txt_NguoiXuLy.Text;
                vipham_bll.Update_ViPham(vp, vp, vp, vp, vp, vp);
                XtraMessageBox.Show("Sửa thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            MyGetData(1);
            Huy_Text(this);
            bbi_Them.Enabled = true;
            bbi_Sua.Enabled = true;
            bbi_Luu.Enabled = false;
            bbi_Huy.Enabled = false;
            
        }

        private void bbi_Them_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Flag = 0;
            txt_MaVP.Text = "Mã tự sinh";
            txt_MaVP.Enabled = false;
            bbi_Them.Enabled = false;
            bbi_Sua.Enabled = false;
            bbi_Luu.Enabled = true;
            bbi_Luu.Enabled = true;
            this.Text = "Thêm Mới Vi Phạm";

        }

        private void bbi_Sua_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Flag = 1;
            bbi_Them.Enabled = false;
            bbi_Sua.Enabled = false;
            bbi_Luu.Enabled = true;
            bbi_Luu.Enabled = true;
            this.Text = "Sửa Đổi Vi Phạm";
        }

        private void bbi_Huy_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Huy_Text(this);
            txt_MaVP.Enabled = false;
            bbi_Them.Enabled = true;
            bbi_Sua.Enabled = true;
            bbi_Luu.Enabled = false;
            bbi_Luu.Enabled = false;
            Flag = 4;
        }

        private void bbi_Thoat_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }

        private void txt_MaVP_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsControl(e.KeyChar) && !Char.IsDigit(e.KeyChar))
                e.Handled = true;
        }

        private void txt_MaDocGia_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsControl(e.KeyChar) && !Char.IsDigit(e.KeyChar))
                e.Handled = true;
        }

        private void txt_MaVP_Leave(object sender, EventArgs e)
        {
            
        }
    }
}